﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace _04SearchForSubstring
{
    class Program
    {
        static void Main(string[] args)
        {
            string myString = "In the town where I was born, Lived a man who sailed to sea, And he told us of his life, In the land of submarines, So we sailed on to the   sun, Till we found the sea green, And we lived beneath the waves, In our yellow submarine, We all live in a yellow submarine, yellow submarine, yellow submarine, We all live in a yellow submarine, yellow submarine, yellow submarine.";
            string subString = "submarine";
            Console.WriteLine("using my method:{0}", SubstringSearch(myString, subString));
            Console.WriteLine("using RegularExpressions:{0}",Regex.Matches(myString, subString, RegexOptions.IgnoreCase).Count);
        }
        static int SubstringSearch(string str, string subStr)
        {
            int count = 0;
            for (int i = 0; i < str.Length - subStr.Length; i++)
            {
                if (str.Substring(i, subStr.Length).ToLower() == subStr.ToLower())
                {
                    count++;
                }
            }
            return count;            
        }
    }
}